﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;

namespace FACUI.BasicClass
{
    public class MapData
    {
        private ClassBase _allItems = new ClassBase(new List<Sharp>());
        public ClassBase _logicDigits = new ClassBase(new Dictionary<string, Logic>());
        public List<Quduan> allQuduan = new List<Quduan>();
        public int[][][] array4checkLine;
        public MapSet ct = new MapSet();
        private Dictionary<XHJ, Quduan> dictFh = new Dictionary<XHJ, Quduan>();
        private Dictionary<XHJ, int> dictFx = new Dictionary<XHJ, int>();
        private Dictionary<XHJ, Quduan> dictJj = new Dictionary<XHJ, Quduan>();
        public Dictionary<int, JYJ> dictJyj = new Dictionary<int, JYJ>();
        public Dictionary<JYJ, GroupInfo> dicGroup = new Dictionary<JYJ, GroupInfo>();
        public int iTxtColumnCount = 3;
        public static Dictionary<Sharp, Pen> dict区段颜色 = new Dictionary<Sharp, Pen>();
        public Rectangle drawRect;
        public HotPoint hotPoint;
        public Hashtable hotPoint2sharpItem;
        public static Dictionary<string, string> lianxiang;
        public static Dictionary<string, bool> lianxiangneed;
        public Dictionary<string, Logic> logicDigitsTemp;
        public Size maxWorkArea;
        public bool needDrawRect;
        public bool needNewItem;
        public CfgNode nodeDC;
        public CfgNode nodeDCBS;
        public CfgNode nodeCGQ;
        public CfgNode nodeGroupInfo;
        public CfgNode nodeDHAN;
        public CfgNode nodeExtern;
        public CfgNode nodeGaishu;
        public CfgNode nodeJL;
        public CfgNode nodeLogicKgl;
        public CfgNode nodeMap;
        public CfgNode nodeQuduan;
        public CfgNode nodeRoot;
        public CfgNode nodeWCQD;
        public CfgNode nodeXHJ;
        public CfgNode nodeXiangshu;
        public CfgNode node信号机区段开关量;
        public List<SharpOffset> perItems;
        public MapStack ReDoStack = new MapStack();
        public float scale;
        public List<Sharp> selItems;
        public static Rectangle showArea = new Rectangle();
        public Rectangle Rectxt = new Rectangle();
        public static int toolbarHeight;
        public static int 画网格的最低标准 = 8;
        public static bool 加强显示所选控件 = false;
        public static bool 开始打印 = false;
        private List<string> 临时道岔名 = new List<string>();
        private List<Sharp> 临时区段控件 = new List<Sharp>();
        private List<string> 临时区段名 = new List<string>();
        private List<Sharp> 临时已检索控件 = new List<Sharp>();
        public static bool 需要整体刷新 = false;
        public static string 钻石名称 = "d_ORANGE";

        public MapData(CfgNode nodeMap, CfgNode nodeRoot)
        {
            Sharp.perLinePen.DashStyle = DashStyle.Dot;
            this.nodeMap = nodeMap;
            this.nodeRoot = nodeRoot;
            if (this.nodeMap == null)
            {
                this.nodeMap = new CfgNode("站场图根结点");
            }
            this.logicDigits = new Dictionary<string, Logic>();
            this.logicDigitsTemp = new Dictionary<string, Logic>();
            this.allItems = new List<Sharp>();
            lianxiang = new Dictionary<string, string>();
            lianxiangneed = new Dictionary<string, bool>();
            lianxiang.Add("XHJ_GB", "-GB");
            lianxiangneed.Add("XHJ_GB", true);
            lianxiang.Add("XHJ_QDFX", "_QDFX");
            lianxiang.Add("XHJ_DPL6", "-L6");
            lianxiang.Add("XHJ_DPL5", "-L5");
            lianxiang.Add("XHJ_DPL4", "-L4");
            lianxiang.Add("XHJ_DPL3", "-L3");
            lianxiang.Add("XHJ_DPL2", "-L2");
            lianxiang.Add("XHJ_DPL", "-L");
            lianxiang.Add("XHJ_DPLU", "-LU");
            lianxiang.Add("XHJ_DPLU2", "-LU2");
            lianxiang.Add("XHJ_DPU", "-U");
            lianxiang.Add("XHJ_DPU2", "-U2");
            lianxiang.Add("XHJ_DPU2S", "-U2S");
            lianxiang.Add("XHJ_DPUU", "-UU");
            lianxiang.Add("XHJ_DPUUS", "-UUS");
            lianxiang.Add("XHJ_DPHU", "-HU");
            lianxiang.Add("XHJ_DPHB", "-HB");
            lianxiang.Add("XHJ_DPH", "-H");
            lianxiang.Add("XHJ_DPZY", "-ZY");
            lianxiang.Add("XHJ_DPZP", "-ZP");
            lianxiangneed.Add("XHJ_QDFX", true);
            lianxiangneed.Add("XHJ_DPL6", true);
            lianxiangneed.Add("XHJ_DPL5", true);
            lianxiangneed.Add("XHJ_DPL4", true);
            lianxiangneed.Add("XHJ_DPL3", true);
            lianxiangneed.Add("XHJ_DPL2", true);
            lianxiangneed.Add("XHJ_DPL", true);
            lianxiangneed.Add("XHJ_DPLU", true);
            lianxiangneed.Add("XHJ_DPLU2", true);
            lianxiangneed.Add("XHJ_DPU", true);
            lianxiangneed.Add("XHJ_DPU2", true);
            lianxiangneed.Add("XHJ_DPU2S", true);
            lianxiangneed.Add("XHJ_DPUU", true);
            lianxiangneed.Add("XHJ_DPUUS", true);
            lianxiangneed.Add("XHJ_DPHU", true);
            lianxiangneed.Add("XHJ_DPHB", true);
            lianxiangneed.Add("XHJ_DPH", true);
            lianxiangneed.Add("XHJ_DPZY", true);
            lianxiangneed.Add("XHJ_DPZP", true);
            this.scale = 1f;
            this.hotPoint = null;
            this.hotPoint2sharpItem = new Hashtable();
            this.selItems = new List<Sharp>();
            this.selItems.Clear();
            this.needNewItem = false;
            this.perItems = new List<SharpOffset>();
        }

        public virtual void addItem(Sharp s)
        {
            this.ReDoStack.Push(OperType.List添加, this._allItems, -1, s);
            this.allItems.Add(s);
        }

        public virtual void addSel(Sharp e)
        {
            if (!e.isSel)
            {
                this.selItems.Add(e);
                e.isSel = true;
            }
        }

        public virtual void alignment(char c)
        {
            int pointY = 0x1388;
            int num2 = 0;
            int pointX = 0x1388;
            int num4 = 0;
            foreach (Sharp sharp in this.selItems)
            {
                if (sharp.pointX < pointX)
                {
                    pointX = sharp.pointX;
                }
                if (sharp.pointY < pointY)
                {
                    pointY = sharp.pointY;
                }
                if ((sharp.pointY + sharp.rectReal.Height) > num2)
                {
                    num2 = sharp.pointY + sharp.rectReal.Height;
                }
                if ((sharp.pointX + sharp.rectReal.Width) > num4)
                {
                    num4 = sharp.pointX + sharp.rectReal.Width;
                }
            }
            foreach (Sharp sharp in this.selItems)
            {
                if ('T' == c)
                {
                    sharp.move(new Point(0, pointY - sharp.pointY));
                }
                if ('L' == c)
                {
                    sharp.move(new Point(pointX - sharp.pointX, 0));
                }
                if ('B' == c)
                {
                    sharp.move(new Point(0, (num2 - sharp.pointY) - sharp.rectReal.Height));
                }
                if ('R' == c)
                {
                    sharp.move(new Point((num4 - sharp.pointX) - sharp.rectReal.Width, 0));
                }
            }
        }

        public virtual bool checkLine(Graphics g)
        {
            int num3;
            int num4;
            int num5;
            int num = this.ct.mapsize.Width / this.ct.gridsize.Width;
            int num2 = this.ct.mapsize.Height / this.ct.gridsize.Height;
            this.array4checkLine = new int[num][][];
            for (num3 = 0; num3 < num; num3++)
            {
                this.array4checkLine[num3] = new int[num2][];
                num4 = 0;
                while (num4 < num2)
                {
                    this.array4checkLine[num3][num4] = new int[4];
                    num5 = 0;
                    while (num5 < 4)
                    {
                        this.array4checkLine[num3][num4][num5] = 0;
                        num5++;
                    }
                    num4++;
                }
            }
            foreach (Sharp sharp in this.allItems)
            {
                sharp.checkLine(this.array4checkLine);
            }
            Pen pen = new Pen(Color.Red, 2f);
            for (num3 = 0; num3 < num; num3++)
            {
                num4 = 0;
                while (num4 < num2)
                {
                    num5 = 0;
                    while (num5 < 4)
                    {
                        if (this.array4checkLine[num3][num4][0] > 1)
                        {
                            g.DrawLine(pen, (int)((this.ct.gridsize.Width * num3) + this.ct.offset.X), (int)((this.ct.gridsize.Height * num4) + this.ct.offset.Y), (int)((this.ct.gridsize.Width * (num3 + 1)) + this.ct.offset.X), (int)((this.ct.gridsize.Height * num4) + this.ct.offset.Y));
                        }
                        if (this.array4checkLine[num3][num4][1] > 1)
                        {
                            g.DrawLine(pen, (int)((this.ct.gridsize.Width * num3) + this.ct.offset.X), (int)((this.ct.gridsize.Height * num4) + this.ct.offset.Y), (int)((this.ct.gridsize.Width * (num3 + 1)) + this.ct.offset.X), (int)((this.ct.gridsize.Height * (num4 + 1)) + this.ct.offset.Y));
                        }
                        if (this.array4checkLine[num3][num4][2] > 1)
                        {
                            g.DrawLine(pen, (int)((this.ct.gridsize.Width * num3) + this.ct.offset.X), (int)((this.ct.gridsize.Height * num4) + this.ct.offset.Y), (int)((this.ct.gridsize.Width * num3) + this.ct.offset.X), (int)((this.ct.gridsize.Height * (num4 + 1)) + this.ct.offset.Y));
                        }
                        if (this.array4checkLine[num3][num4][3] > 1)
                        {
                            g.DrawLine(pen, (int)((this.ct.gridsize.Width * num3) + this.ct.offset.X), (int)((this.ct.gridsize.Height * num4) + this.ct.offset.Y), (int)((this.ct.gridsize.Width * (num3 - 1)) + this.ct.offset.X), (int)((this.ct.gridsize.Height * (num4 + 1)) + this.ct.offset.Y));
                        }
                        num5++;
                    }
                    num4++;
                }
            }
            for (num3 = 0; num3 < num; num3++)
            {
                for (num4 = 0; num4 < num2; num4++)
                {
                    for (num5 = 0; num5 < 4; num5++)
                    {
                        if (this.array4checkLine[num3][num4][num5] > 1)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        public bool 初始化所有区段(Pen pen)
        {
            int num = 0;
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbDC".Equals(sharp.sharpType) || "sbWCQD".Equals(sharp.sharpType))
                {
                    dict区段颜色.Remove(sharp);
                    dict区段颜色.Add(sharp, pen);
                    num++;
                }
            }
            return (num > 0);
        } 

        public virtual void clearAllLogic()
        {
            List<Sharp> list = new List<Sharp>();
            foreach (Logic logic in this.logicDigits.Values)
            {
                this.ReDoStack.Push(OperType.Dict删除, this._logicDigits, logic.name, logic);
            }
            this.logicDigits.Clear();
            foreach (Sharp sharp in this.allItems)
            {
                foreach (Digit digit in sharp.digitList)
                {
                    if (digit.isLogic)
                    {
                        digit.isLogic = false;
                        digit.name = "";
                    }
                }
            }
            this.SignModify();
        }

        public virtual void clearSel()
        {
            foreach (Sharp sharp in this.allItems)
            {
                sharp.isSel = false;
            }
            this.selItems.Clear();
        }

        public virtual int clearSelKgl()
        {
            List<Sharp> list = new List<Sharp>();
            this.logicDigits.Clear();
            int num = 0;
            int num2 = 0;
            foreach (Sharp sharp in this.selItems)
            {
                foreach (Digit digit in sharp.digitList)
                {
                    if ((digit.name != null) && (digit.name.Trim().Length > 0))
                    {
                        num++;
                        if (digit.isLogic)
                        {
                            num2++;
                        }
                    }
                    digit.isLogic = false;
                    digit.name = "";
                }
            }
            this.SignModify();
            return ((num2 * 0xf4240) + num);
        }

        public virtual bool copy()
        {
            StringBuilder builder = new StringBuilder("[详述]");
            int num = 0;
            foreach (Sharp sharp in this.selItems)
            {
                num++;
                builder.AppendLine().Append("控件描述").Append(num.ToString().PadRight(1)).Append("=").Append(sharp.toStringXiangshu());
            }
            try
            {
                Clipboard.SetText(builder.ToString());
                return true;
            }
            catch (Exception exception)
            {
                Log.appendLog(exception.ToString(), "");
                return false;
            }
        }

        public int createExtern(int sharpPos, string 扩展属性)
        {
            int num = 0;
            if ((扩展属性 == null) || (扩展属性.Length <= 0))
            {
                return num;
            }
            string[] strArray = 扩展属性.Split(new char[] { ',' });
            int count = this.allQuduan.Count;
            int result = -1;
            if (int.TryParse(strArray[1], out result) && ((result >= 0) && (result < count)))
            {
                ((List<Sharp>) this._allItems.value)[sharpPos].quduanAd = result;
                ((List<Sharp>) this._allItems.value)[sharpPos].所属区段名称 = this.allQuduan[result].name;
                if ("sbDC".Equals(strArray[0]))
                {
                    this.allQuduan[result].lst道岔.Add((DC) ((List<Sharp>) this._allItems.value)[sharpPos]);
                }
                else if ("sbWCQD".Equals(strArray[0]))
                {
                    this.allQuduan[result].lst无岔区段.Add((WCQD) ((List<Sharp>) this._allItems.value)[sharpPos]);
                }
            }
            return 1;
        }

        public static Sharp createItem(MapData mapData, string str)
        {
            Sharp sharp = null;
            try
            {
                string[] strArray = str.Split(new char[] { ',' });
                for (int i = 0; i < strArray.Length; i++)
                {
                    strArray[i] = strArray[i].Trim();
                }
                if ("sbWCQD".Equals(strArray[1]))
                {
                    sharp = new WCQD(mapData, new Point(int.Parse(strArray[5]), int.Parse(strArray[6])), int.Parse(strArray[4]), strArray[2]);
                }
                else if ("sbDC".Equals(strArray[1]))
                {
                    sharp = new DC(mapData, new Point(int.Parse(strArray[5]), int.Parse(strArray[6])), int.Parse(strArray[4]), strArray[2]);
                }
                else if ("sbXHJ".Equals(strArray[1]))
                {
                    sharp = new XHJ(mapData, new Point(int.Parse(strArray[5]), int.Parse(strArray[6])), int.Parse(strArray[4]), strArray[2]);
                }
                else if ("sbJYJ".Equals(strArray[1]))
                {
                    sharp = new JYJ(mapData, new Point(int.Parse(strArray[5]), int.Parse(strArray[6])), int.Parse(strArray[4]), strArray[2]);
                }                
                else if ("sbDHAN".Equals(strArray[1]))
                {
                    int x = int.Parse(strArray[5]);
                    int y = int.Parse(strArray[6]);
                    if ((strArray[2] == "-1") || (strArray[2] == "-2"))
                    {
                        y++;
                    }
                    sharp = new DHAN(mapData, new Point(x, y), int.Parse(strArray[4]), strArray[2]);
                }
                else if ("sbWB".Equals(strArray[1]))
                {
                    sharp = new WB(mapData, new Point(int.Parse(strArray[5]), int.Parse(strArray[6])), int.Parse(strArray[4]), strArray[2]);
                }
                if (sharp != null)
                {
                    sharp.text = strArray[0];
                    sharp.sizeDefineWidth = int.Parse(strArray[7]);
                    sharp.sizeDefineHeight = int.Parse(strArray[8]);
                    sharp.txtSize = int.Parse(strArray[9]);
                    Color color = Color.FromArgb(0xff, Color.FromArgb(int.Parse(strArray[3])));
                    sharp.lineColor = Color.FromArgb(color.A, color.B, color.G, color.R);
                    color = Color.FromArgb(0xff, Color.FromArgb(int.Parse(strArray[10])));
                    sharp.txtColor = Color.FromArgb(color.A, color.B, color.G, color.R);
                    sharp.txtFont = strArray[11];
                    sharp.isHideTxt = "1".Equals(strArray[12]);
                }
            }
            catch (Exception exception)
            {
                Log.appendLog(exception.ToString(), "");
            }
            return sharp;
        }

        public static Logic createLogic(string str)
        {
            Logic logic = null;
            try
            {
                int num;
                str = str.Substring(str.IndexOf('=') + 1);
                string[] strArray = str.Split(new char[] { ',' });
                for (int i = 0; i < strArray.Length; i++)
                {
                    strArray[i] = strArray[i].Trim();
                }
                logic = new Logic(strArray[0]) {
                    oper = int.Parse(strArray[1]),
                    d1 = strArray[3].Split(new char[] { ';' })[0],
                    d2 = strArray[4].Split(new char[] { ';' })[0],
                    d3 = strArray[5].Split(new char[] { ';' })[0],
                    d1Not = "1".Equals(strArray[3].Split(new char[] { ';' })[1]),
                    d2Not = "1".Equals(strArray[4].Split(new char[] { ';' })[1]),
                    d3Not = "1".Equals(strArray[5].Split(new char[] { ';' })[1]),
                    d1Logic = "1".Equals(strArray[3].Split(new char[] { ';' })[2]),
                    d2Logic = "1".Equals(strArray[4].Split(new char[] { ';' })[2]),
                    d3Logic = "1".Equals(strArray[5].Split(new char[] { ';' })[2])
                };
                int.TryParse(strArray[3].Split(new char[] { ';' })[3], out num);
                logic.ad1 = num;
                int.TryParse(strArray[4].Split(new char[] { ';' })[3], out num);
                logic.ad2 = num;
                int.TryParse(strArray[5].Split(new char[] { ';' })[3], out num);
                logic.ad3 = num;
                if (logic == null)
                {
                }
            }
            catch (Exception exception)
            {
                Log.appendLog(exception.ToString(), "");
            }
            return logic;
        }

        public virtual List<Logic> createLogicDigits(string desc, string type)
        {
            List<Logic> list = new List<Logic>();
            Logic item = null;
            int num = -1;
            int num2 = -1;
            string[] strArray = desc.Split(new char[] { '\n' });
            if ((strArray != null) && (strArray.Length > 0))
            {
                int num3;
                for (num3 = 0; num3 < strArray.Length; num3++)
                {
                    if (strArray[num3].StartsWith("[逻辑开关量]"))
                    {
                        num = num3;
                    }
                }
                for (num3 = num + 1; num3 < strArray.Length; num3++)
                {
                    num2 = num3;
                    if (strArray[num3].StartsWith("["))
                    {
                        break;
                    }
                }
                if (((num >= 0) && (num2 >= 0)) && (num2 >= num))
                {
                    for (num3 = num; num3 <= num2; num3++)
                    {
                        item = null;
                        string str = strArray[num3].Trim();
                        if (!"".Equals(str) && "TJWX2000".Equals(type))
                        {
                            try
                            {
                                item = createLogic(str.Substring(str.IndexOf('=') + 1));
                                if (item != null)
                                {
                                    list.Add(item);
                                }
                            }
                            catch (Exception exception)
                            {
                                Log.appendLog(exception.ToString(), "");
                            }
                        }
                    }
                }
            }
            return list;
        }

        public virtual List<Sharp> createSharp(string desc, string type)
        {
            List<Sharp> list = new List<Sharp>();
            Sharp item = null;
            string[] strArray = desc.Split(new char[] { '\n' });
            if ((strArray != null) && (strArray.Length > 0))
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    item = null;
                    string str = strArray[i].Trim();
                    if ((!"".Equals(str) && "TJWX2000".Equals(type)) && (str.IndexOf("控件描述") == 0))
                    {
                        str = str.Substring(str.IndexOf('=') + 1);
                        item = createItem(this, str);
                        if (null != item)
                        {
                            list.Add(item);
                        }
                    }
                }
            }
            return list;
        }
                
        public virtual List<Sharp> find(string str, string type)
        {
            List<Sharp> list = new List<Sharp>();
            foreach (Sharp sharp in this.allItems)
            {
                if (((sharp.text.IndexOf(str.ToLower()) >= 0) || (sharp.text.IndexOf(str.ToUpper()) >= 0)) && ((type == null) || sharp.sharpType.Equals(type)))
                {
                    list.Add(sharp);
                }
            }
            return list;
        }

        public virtual Sharp getClickedItem(Point p)
        {
            Sharp sharp = null;
            double num = 300000.0;
            double num2 = 300000.0;
            p.X -= this.ct.offset.X;
            p.Y -= this.ct.offset.Y;
            foreach (Sharp sharp2 in this.allItems)
            {
                if (sharp2.isClickedItem(p, this.ct.gridsize))
                {
                    num2 = sharp2.distance(p, this.ct.gridsize);
                    if (num > num2)
                    {
                        num = num2;
                        sharp = sharp2;
                    }
                }
            }
            return sharp;
        }

        public static int getDcDigitId(CfgNode maproot, string name, int type)
        {
            int result = -1;
            bool flag = false;
            CfgNode subNode = maproot.GetSubNode("道岔");
            CfgNode node2 = maproot.GetSubNode("概述");
            if ((node2 == null) || (subNode == null))
            {
                return -1;
            }
            int valueInt = node2.GetValueInt("道岔数目");
            string str = null;
            string[] strArray = null;
            string str2 = null;
            bool flag2 = false;
            for (int i = 1; i <= valueInt; i++)
            {
                str = subNode.GetValue(i.ToString());
                if (str != null)
                {
                    strArray = str.Split(new char[] { ',' });
                    if (((strArray != null) && (strArray.Length > 5)) && name.ToUpper().Equals(strArray[1]))
                    {
                        flag2 = true;
                        break;
                    }
                }
            }
            if (!flag2)
            {
                return -1;
            }
            strArray = strArray[type + 1].Split(new char[] { ';' });
            if ((strArray != null) && (strArray.Length >= 3))
            {
                str2 = strArray[0];
                flag = "1".Equals(strArray[1]);
                if (!int.TryParse(strArray[2], out result))
                {
                    return -1;
                }
            }
            if (flag)
            {
                result += 0x8000;
            }
            if ((str2 == null) || "".Equals(str2))
            {
                return -1;
            }
            return result;
        }

        public virtual HotPoint getHotCursor(Point p)
        {
            foreach (DictionaryEntry entry in this.hotPoint2sharpItem)
            {
                Rectangle key = (Rectangle) entry.Key;
                if ((((p.X >= key.Left) && (p.X <= key.Right)) && (p.Y >= key.Top)) && (p.Y <= key.Bottom))
                {
                    return (HotPoint) entry.Value;
                }
            }
            return null;
        }

        public virtual int getOrSetDefaultIntValue(CfgNode n, string key, int defVal)
        {
            int valueInt = n.GetValueInt(key);
            if (0 <= valueInt)
            {
                return valueInt;
            }
            this.nodeGaishu.AddValue(key, defVal);
            return defVal;
        }

        public static int getXhjDigitId(CfgNode maproot, string name, int type)
        {
            int num3;
            int result = -1;
            bool flag = false;
            CfgNode subNode = maproot.GetSubNode("信号机区段开关量");
            CfgNode node2 = maproot.GetSubNode("概述");
            if ((node2 == null) || (subNode == null))
            {
                return -1;
            }
            int valueInt = node2.GetValueInt("信号机数目");
            string str = null;
            string[] strArray = null;
            string str2 = null;
            for (num3 = 0; num3 < valueInt; num3++)
            {
                str = subNode.GetValue(num3.ToString());
                if ((str != null) && str.ToUpper().StartsWith(name.ToUpper() + ","))
                {
                    strArray = str.Split(new char[] { ',' });
                }
            }
            if ((strArray == null) || (strArray.Length <= 0))
            {
                return -1;
            }
            if (type == 1)
            {
                str2 = strArray[1];
                flag = "1".Equals(strArray[2]);
                if (!int.TryParse(strArray[3], out result))
                {
                    result = -1;
                }
            }
            else
            {
                str2 = strArray[4];
                flag = "1".Equals(strArray[5]);
            }
            if ((str2 == null) || "".Equals(str2))
            {
                return -1;
            }
            if (flag)
            {
                CfgNode node3 = maproot.GetSubNode("逻辑开关量");
                int num4 = node2.GetValueInt("逻辑开关量数目");
                for (num3 = 0; num3 < num4; num3++)
                {
                    str = node3.GetValue(num3.ToString());
                    if ((str != null) && str.ToUpper().StartsWith(str2.ToUpper()))
                    {
                        return ((0x8000 + num3) - 1);
                    }
                }
            }
            return result;
        }

        public static string getXhjDigitName(CfgNode maproot, string name, int type)
        {
            CfgNode subNode = maproot.GetSubNode("信号机区段开关量");
            CfgNode node2 = maproot.GetSubNode("概述");
            if ((node2 == null) || (subNode == null))
            {
                return "";
            }
            int valueInt = node2.GetValueInt("信号机数目");
            string str = null;
            string[] strArray = null;
            string str2 = "";
            for (int i = 0; i < valueInt; i++)
            {
                str = subNode.GetValue(i.ToString());
                if ((str != null) && str.ToUpper().StartsWith(name.ToUpper() + ","))
                {
                    strArray = str.Split(new char[] { ',' });
                }
            }
            if ((strArray == null) || (strArray.Length <= 0))
            {
                return "";
            }
            if (type == 1)
            {
                str2 = strArray[1];
            }
            else
            {
                str2 = strArray[4];
            }
            return str2;
        }

        public bool IsModified()
        {
            return this.ReDoStack.isModified;
        }

        public virtual List<Digit2Item> kglCheck(string digitPath)
        {
            int num3;
            this.SignModify();
            List<Digit2Item> list = new List<Digit2Item>();
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            Dictionary<string, int> dictionary2 = new Dictionary<string, int>();
            CfgNode nodeDirect = null;
            int num = 0;
            int num2 = 0xffff;
            string str = "";
            string[] strArray = null;
            if (this.nodeRoot == null)
            {
                if ((digitPath == null) || (digitPath.Trim().Length == 0))
                {
                    MessageBox.Show("没有找到开关量文件名！", "错误");
                }
                IniFile file = new IniFile(digitPath);
                file.Load();
                try
                {
                    num = int.Parse(file.GetString("开关量", "总路数"));
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    Log.appendLog(exception.ToString(), "");
                }
                for (num3 = 1; num3 <= num; num3++)
                {
                    str = file.GetString("开关量", num3.ToString());
                    if ((str != null) && (str.Length > 0))
                    {
                        strArray = str.Split(new char[] { ',' });
                    }
                    if ((strArray != null) && (strArray.Length > 2))
                    {
                        num2 = 0xffff;
                        try
                        {
                            num2 = int.Parse(strArray[1].Trim());
                        }
                        catch (Exception exception2)
                        {
                            Log.appendLog(exception2.ToString(), "");
                        }
                    }
                    if (!dictionary.ContainsKey(strArray[0].Trim()))
                    {
                        dictionary.Add(strArray[0].Trim(), num2);
                    }
                }
            }
            else
            {
                nodeDirect = this.nodeRoot.GetNodeDirect(@"开关量\实际开关量");
                if (nodeDirect != null)
                {
                    foreach (CfgNode node2 in nodeDirect.GetSubNodeList())
                    {
                        if (!dictionary.ContainsKey(node2["开关量名称"]))
                        {
                            dictionary.Add(node2.GetValue("开关量名称"), node2.GetValueInt("AD号"));
                        }
                    }
                }
            }
            int num4 = this.getOrSetDefaultIntValue(this.nodeGaishu, "逻辑开关量数目", 0);
            string key = null;
            for (num3 = 1; num3 <= num4; num3++)
            {
                num2 = 0xffff;
                str = this.nodeLogicKgl.GetValue(num3.ToString());
                if ((str != null) && (str.Length > 0))
                {
                    strArray = str.Split(new char[] { ',' });
                }
                if ((strArray != null) && (strArray.Length > 2))
                {
                    key = strArray[0].Trim();
                    num2 = num3 - 1;
                }
                dictionary2.Remove(key);
                dictionary2.Add(key, num2);
            }
            int num5 = 0xffff;
            string str3 = "";
            foreach (Sharp sharp in this.allItems)
            {
                foreach (Digit digit in sharp.digitList)
                {
                    str3 = digit.name.Trim();
                    if (str3.Length > 0)
                    {
                        if (!digit.isLogic)
                        {
                            if (dictionary.TryGetValue(str3, out num5))
                            {
                                digit.ad = num5;
                            }
                            else
                            {
                                digit.ad = 0xffff;
                                list.Add(new Digit2Item(digit, sharp, false));
                            }
                        }
                        else if (dictionary2.TryGetValue(str3, out num5))
                        {
                            digit.ad = num5;
                        }
                        else
                        {
                            digit.ad = 0xffff;
                            list.Add(new Digit2Item(digit, sharp, false));
                        }
                    }
                    else
                    {
                        digit.ad = 0xffff;
                    }
                }
            }
            foreach (Logic logic in this.logicDigits.Values)
            {
                if (logic != null)
                {
                    Digit2Item item;
                    if (logic.d1.Trim().Length > 0)
                    {
                        if (!logic.d1Logic)
                        {
                            if (dictionary.TryGetValue(logic.d1.Trim(), out num5))
                            {
                                logic.ad1 = num5;
                            }
                            else
                            {
                                item = new Digit2Item(logic, logic.d1, true);
                                list.Add(item);
                            }
                        }
                        else if (dictionary2.TryGetValue(logic.d1.Trim(), out num5))
                        {
                            logic.ad1 = num5;
                        }
                        else
                        {
                            item = new Digit2Item(logic, logic.d1, true);
                            list.Add(item);
                        }
                    }
                    if (logic.d2.Trim().Length > 0)
                    {
                        if (!logic.d2Logic)
                        {
                            if (dictionary.TryGetValue(logic.d2.Trim(), out num5))
                            {
                                logic.ad2 = num5;
                            }
                            else
                            {
                                item = new Digit2Item(logic, logic.d2, true);
                                list.Add(item);
                            }
                        }
                        else if (dictionary2.TryGetValue(logic.d2.Trim(), out num5))
                        {
                            logic.ad2 = num5;
                        }
                        else
                        {
                            item = new Digit2Item(logic, logic.d2, true);
                            list.Add(item);
                        }
                    }
                    if (logic.d3.Trim().Length > 0)
                    {
                        if (!logic.d3Logic)
                        {
                            if (dictionary.TryGetValue(logic.d3.Trim(), out num5))
                            {
                                logic.ad3 = num5;
                            }
                            else
                            {
                                item = new Digit2Item(logic, logic.d3, true);
                                list.Add(item);
                            }
                        }
                        else if (dictionary2.TryGetValue(logic.d3.Trim(), out num5))
                        {
                            logic.ad3 = num5;
                        }
                        else
                        {
                            item = new Digit2Item(logic, logic.d3, true);
                            list.Add(item);
                        }
                    }
                }
            }
            WCQD wcqd = null;
            WCQD wcqd2 = null;
            StringBuilder builder = null;
            num3 = 0;
            Digit digit2 = null;
            foreach (Sharp sharp in this.allItems)
            {
                builder = new StringBuilder();
                if ("sbXHJ".Equals(sharp.sharpType))
                {
                    int num7;
                    string name = "";
                    string str5 = "";
                    string str6 = "";
                    string str7 = "";
                    int num6 = num7 = -1;
                    this.寻找信号机区段((XHJ) sharp, out wcqd, out wcqd2);
                    if (wcqd != null)
                    {
                        digit2 = wcqd.getDigit("WCQD_H");
                        if (digit2 != null)
                        {
                            name = digit2.name;
                            str5 = digit2.isLogic ? "1" : "0";
                            if (!digit2.isLogic && !dictionary.TryGetValue(digit2.name, out num6))
                            {
                                num6 = -1;
                            }
                        }
                    }
                    if (wcqd2 != null)
                    {
                        digit2 = wcqd2.getDigit("WCQD_H");
                        if (digit2 != null)
                        {
                            str6 = digit2.name;
                            str7 = digit2.isLogic ? "1" : "0";
                            if (!digit2.isLogic && !dictionary.TryGetValue(digit2.name, out num7))
                            {
                                num7 = -1;
                            }
                        }
                    }
                    builder.Append(sharp.text).Append(',').Append(name).Append(',').Append(str5).Append(',').Append(num6).Append(',').Append(str6).Append(',').Append(str7).Append(',').Append(num6);
                    this.node信号机区段开关量.AddValue(num3.ToString(), builder.ToString());
                    num3++;
                }
            }
            foreach (Sharp sharp in this.allItems)
            {
                builder = new StringBuilder();
                if ("sbXHJ".Equals(sharp.sharpType))
                {
                    builder.Append(sharp.text).Append('=').Append(getXhjDigitId(this.nodeMap, sharp.text, 1)).Append(',').Append(getXhjDigitId(this.nodeMap, sharp.text, 0)).AppendLine();
                }
            }
            return list;
        }

        public virtual List<Sharp> kgl未添加开关量控件集合()
        {
            List<Sharp> list = new List<Sharp>();
            bool flag = false;
            foreach (Sharp sharp in this.allItems)
            {
                flag = false;
                foreach (Digit digit in sharp.digitList)
                {
                    if (digit.name.Length > 0)
                    {
                        flag = true;
                        break;
                    }
                }
                if (sharp.digitList.Count == 0)
                {
                    flag = true;
                }
                if (!flag)
                {
                    list.Add(sharp);
                }
            }
            return list;
        }

        public virtual void loadFile(string filename)
        {
            int num11;
            IniFile iniFile = new IniFile(filename);
            iniFile.Load();
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "逻辑开关量数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "控件总数");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "道岔数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "信号机数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "无岔区段数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "灯或按钮数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "道岔表示数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "绝缘节数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "传感器数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "文本数目");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "线宽");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "屏幕宽度");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "屏幕高度");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "X网格大小");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "Y网格大小");
            this.loadOneItem(this.nodeGaishu, iniFile, "概述", "区段数目");
            int cnt = this.getOrSetDefaultIntValue(this.nodeGaishu, "逻辑开关量数目", 0);
            int num2 = this.getOrSetDefaultIntValue(this.nodeGaishu, "控件总数", 0);
            int num3 = this.getOrSetDefaultIntValue(this.nodeGaishu, "道岔数目", 0);
            int num4 = this.getOrSetDefaultIntValue(this.nodeGaishu, "信号机数目", 0);
            int num5 = this.getOrSetDefaultIntValue(this.nodeGaishu, "无岔区段数目", 0);
            int num6 = this.getOrSetDefaultIntValue(this.nodeGaishu, "灯或按钮数目", 0);
            int num7 = this.getOrSetDefaultIntValue(this.nodeGaishu, "道岔表示数目", 0);
            int num8 = this.getOrSetDefaultIntValue(this.nodeGaishu, "绝缘节数目", 0);
            int num9 = this.getOrSetDefaultIntValue(this.nodeGaishu, "文本数目", 0);
            int num10 = this.getOrSetDefaultIntValue(this.nodeGaishu, "区段数目", 0);
            int num20 = this.getOrSetDefaultIntValue(this.nodeGaishu, "传感器数目", 0);
            int numGroup = this.getOrSetDefaultIntValue(this.nodeGaishu, "传感器分组数目", 0);
            this.loadOneSection(this.nodeXiangshu, iniFile, "详述", "控件描述", num2);
            this.loadOneSection(this.nodeDC, iniFile, "道岔", "", num3);
            this.loadOneSection(this.nodeXHJ, iniFile, "信号机", "", num4);
            this.loadOneSection(this.nodeWCQD, iniFile, "无岔区段", "", num5);
            this.loadOneSection(this.nodeDHAN, iniFile, "灯或按钮", "", num6);
            this.loadOneSection(this.nodeDCBS, iniFile, "道岔表示", "", num7);
            this.loadOneSection(this.nodeCGQ, iniFile, "传感器", "", num20);
            this.loadOneSection(this.nodeGroupInfo, iniFile, "传感器分组", "", numGroup = this.dicGroup.Count);
            this.loadOneSection(this.nodeLogicKgl, iniFile, "逻辑开关量", "", cnt);
            this.loadOneSection(this.node信号机区段开关量, iniFile, "信号机区段开关量", "", num4);
            for (num11 = 1; num11 <= num10; num11++)
            {
                this.nodeQuduan.AddValue(num11.ToString(), "");
            }
            this.loadOneSection(this.nodeQuduan, iniFile, "区段", "", num10);
            for (num11 = 1; num11 <= num2; num11++)
            {
                this.nodeExtern.AddValue(num11.ToString(), "");
            }
            this.loadOneSection(this.nodeExtern, iniFile, "扩展属性", "", num10);
            this.loadNode();
        }

        public virtual void loadNode()
        {
            int num11;
            int num13;
            int num14;
            int num15;
            int num16;
            this.nodeGaishu = this.nodeMap.GetSubNode("概述", true);
            this.nodeXiangshu = this.nodeMap.GetSubNode("详述", true);
            this.nodeDC = this.nodeMap.GetSubNode("道岔", true);
            this.nodeXHJ = this.nodeMap.GetSubNode("信号机", true);
            this.nodeWCQD = this.nodeMap.GetSubNode("无岔区段", true);
            this.nodeDHAN = this.nodeMap.GetSubNode("灯或按钮", true);
            this.nodeJL = this.nodeMap.GetSubNode("距离", true);
            this.nodeDCBS = this.nodeMap.GetSubNode("道岔表示", true);
            this.nodeCGQ = this.nodeMap.GetSubNode("传感器", true);
            this.nodeGroupInfo = this.nodeMap.GetSubNode("传感器分组", true);
            this.nodeQuduan = this.nodeMap.GetSubNode("区段", true);
            this.node信号机区段开关量 = this.nodeMap.GetSubNode("信号机区段开关量", true);
            this.nodeExtern = this.nodeMap.GetSubNode("扩展属性", true);
            if (this.nodeRoot == null)
            {
                this.ct.name = "NEW";
            }
            int num = this.getOrSetDefaultIntValue(this.nodeGaishu, "逻辑开关量数目", 0);
            int num2 = this.getOrSetDefaultIntValue(this.nodeGaishu, "控件总数", 0);
            int num3 = this.getOrSetDefaultIntValue(this.nodeGaishu, "道岔数目", 0);
            int num4 = this.getOrSetDefaultIntValue(this.nodeGaishu, "信号机数目", 0);
            int num5 = this.getOrSetDefaultIntValue(this.nodeGaishu, "无岔区段数目", 0);
            int num6 = this.getOrSetDefaultIntValue(this.nodeGaishu, "灯或按钮数目", 0);
            int num7 = this.getOrSetDefaultIntValue(this.nodeGaishu, "道岔表示数目", 0);
            int num8 = this.getOrSetDefaultIntValue(this.nodeGaishu, "绝缘节数目", 0);
            int num9 = this.getOrSetDefaultIntValue(this.nodeGaishu, "文本数目", 0);
            int num10 = this.getOrSetDefaultIntValue(this.nodeGaishu, "区段数目", 0);
            int num20 = this.getOrSetDefaultIntValue(this.nodeGaishu, "传感器数目", 0);
            int numGroup = this.getOrSetDefaultIntValue(this.nodeGaishu, "传感器分组数目", 0);
            this.ct.lineWidth = this.getOrSetDefaultIntValue(this.nodeGaishu, "线宽", 2);
            this.ct.oraMapsize.Width = this.getOrSetDefaultIntValue(this.nodeGaishu, "屏幕宽度", 0x3e8);
            this.ct.oraMapsize.Height = this.getOrSetDefaultIntValue(this.nodeGaishu, "屏幕高度", 700);
            this.ct.oraGridsize = new Size();
            this.ct.oraGridsize.Width = this.getOrSetDefaultIntValue(this.nodeGaishu, "X网格大小", 10);
            this.ct.oraGridsize.Height = this.getOrSetDefaultIntValue(this.nodeGaishu, "Y网格大小", 10);
            this.resetGrid();
            this.logicDigits.Clear();
            for (num11 = 1; num11 <= num10; num11++)
            {
                string str = this.nodeQuduan.GetValue(num11.ToString());
                if ("".Equals(str))
                {
                    this.allQuduan.Add(new Quduan(num11 - 1));
                }
                else
                {
                    this.allQuduan.Add(new Quduan(str));
                }
            }
            for (num11 = 1; num11 <= num; num11++)
            {
                Logic logic = createLogic(this.nodeLogicKgl.GetValue(num11.ToString()));
                if (null != logic)
                {
                    if (this.logicDigits.ContainsKey(logic.name))
                    {
                        this.logicDigits.Add(string.Concat(new object[] { logic.name, "[复", num11, "]" }), logic);
                    }
                    else
                    {
                        this.logicDigits.Add(logic.name, logic);
                    }
                }
            }
            Dictionary<string, GroupInfo> tmpDic = new Dictionary<string, GroupInfo>();
            for (int ngi = 1; ngi < numGroup + 1; ngi++)
            {
                GroupInfo gi = new GroupInfo(this.nodeGroupInfo.GetValue(ngi.ToString()));
                tmpDic.Add(gi.strName, gi);
            }
            this.allItems.Clear();
            int num12 = num13 = num14 = num15 = num16 = num20 = 0;
            for (num11 = 1; num11 <= num2; num11++)
            {
                Sharp s = createItem(this, this.nodeXiangshu.GetValue("控件描述" + num11));
                if (s.txtSize < 5)
                {
                    s.txtSize = 5;
                }
                if (null != s)
                {
                    if ("sbDC".Equals(s.sharpType))
                    {
                        num12++;
                        s.createDigit(this.nodeDC.GetValue(num12.ToString()));
                    }
                    if ("sbWCQD".Equals(s.sharpType))
                    {
                        num13++;
                        s.createDigit(this.nodeWCQD.GetValue(num13.ToString()));
                    }
                    if ("sbXHJ".Equals(s.sharpType))
                    {
                        num14++;
                        s.createDigit(this.nodeXHJ.GetValue(num14.ToString()));
                    }
                    if ("sbDHAN".Equals(s.sharpType))
                    {
                        if ("0".Equals(s.subType))
                        {
                            num16++;
                            s.createDigit(this.nodeDCBS.GetValue(num16.ToString()));
                        }
                        else
                        {
                            num15++;
                            s.createDigit(this.nodeDHAN.GetValue(num15.ToString()));
                        }
                    }
                    if ("sbJYJ".Equals(s.sharpType) && ("2".Equals(s.subType)))
                    {
                        GroupInfo gis = null;
                        if (tmpDic.TryGetValue(s.text, out gis))
                        {
                            this.dicGroup.Add((JYJ)s, gis);
                        }
                    }
                    this.addItem(s);
                }
            }
            for (num11 = 1; num11 <= num2; num11++)
            {
                this.createExtern(num11 - 1, this.nodeExtern.GetValue(num11, ""));
            }
            this.maxWorkArea = new Size(0x1388, 0x640);
            if (this.ct.mapsize.Width > this.maxWorkArea.Width)
            {
                this.maxWorkArea.Width = this.ct.mapsize.Width + 0x3e8;
            }
            if (this.ct.mapsize.Height > this.maxWorkArea.Height)
            {
                this.maxWorkArea.Height = this.ct.mapsize.Height + 600;
            }
            this.selItems.Clear();
            this.needNewItem = false;
            this.ReDoStack.Clear();
            this.perItems.Clear();
        }

        private void loadOneItem(CfgNode node, IniFile iniFile, string sectionName, string key)
        {
            node.AddValue(key, iniFile.GetString(sectionName, key));
        }

        private void loadOneSection(CfgNode node, IniFile iniFile, string sectionName, string key, int cnt)
        {
            for (int i = 1; i <= cnt; i++)
            {
                node.AddValue(key + i, iniFile.GetString(sectionName, key + i));
            }
        }

        public virtual bool move(Point to)
        {
            if ((to.X == 0) && (to.Y == 0))
            {
                return false;
            }
            foreach (Sharp sharp in this.selItems)
            {
                sharp.move(to);
            }
            return true;
        }

        public virtual Point moveHot(Point form, Point to)
        {
            Sharp item = this.hotPoint.item;
            int width = (to.X - form.X) / this.ct.gridsize.Width;
            int height = (to.Y - form.Y) / this.ct.gridsize.Height;
            item.addSize(this.hotPoint.direction, this.hotPoint.point, this.hotPoint.width, this.hotPoint.height, width, height);
            if ((height != 0) || (width != 0))
            {
            }
            return new Point(width, height);
        }

        public virtual void onSpaceChar()
        {
            foreach (Sharp sharp in this.selItems)
            {
                sharp.onSpaceChar();
            }
        }

        public virtual void parst(Point p)
        {
            this.perItems.Clear();
            Sharp sharp = null;
            List<Sharp> list = this.createSharp(Clipboard.GetText(), "TJWX2000");
            Point point = new Point(0x3e8, 0x3e8);
            foreach (Sharp sharp2 in list)
            {
                if (sharp2.pointX < point.X)
                {
                    point.X = sharp2.pointX;
                }
                if (sharp2.pointY < point.Y)
                {
                    point.Y = sharp2.pointY;
                }
            }
            point.X *= -1;
            point.Y *= -1;
            foreach (Sharp sharp2 in list)
            {
                sharp = (Sharp) sharp2.Clone();
                Point offset = sharp.point;
                offset.Offset(point);
                offset.Offset(0, 0);
                this.perItems.Add(new SharpOffset(sharp, offset));
            }
            this.needNewItem = true;
        }

        public void Redo()
        {
            this.ReDoStack.ReDo();
        }
        public virtual void reDraw(Graphics g)
        {
            this.hotPoint2sharpItem.Clear();
            Pen pen = null;
            int x = 0xffff;
            int pointY = 0xffff;
            int num3 = -65535;
            int num4 = -65535;
            foreach (Sharp sharp in this.allItems)
            {
                if (sharp.sharpType == "sbJYJ")
                {
                    continue;
                }
                if (x > sharp.pointX)
                {
                    x = sharp.pointX;
                }
                if (pointY > sharp.pointY)
                {
                    pointY = sharp.pointY;
                }
                if (num3 < sharp.pointX)
                {
                    num3 = sharp.pointX + 2;
                }
                if (num4 < sharp.pointY)
                {
                    num4 = sharp.pointY + 5;
                }
                if (!sharp.isMouseOver)
                {
                    if (Sharp.显示模式 == 2)
                    {
                        if (dict区段颜色.TryGetValue(sharp, out pen))
                        {
                            sharp.draw(g, this.ct, pen);
                        }
                        else
                        {
                            sharp.draw(g, this.ct, (Pen)Sharp.默认画笔.Clone());
                        }
                    }
                    else if (Sharp.显示模式 == 1)
                    {
                        sharp.draw(g, this.ct, (Pen)Sharp.默认画笔.Clone());
                    }
                }
            }
            foreach (Sharp sharp in this.allItems)
            {
                if (sharp.sharpType != "sbJYJ")
                {
                    continue;
                }
                if (x > sharp.pointX)
                {
                    x = sharp.pointX;
                }
                if (pointY > sharp.pointY)
                {
                    pointY = sharp.pointY;
                }
                if (num3 < sharp.pointX)
                {
                    num3 = sharp.pointX + 2;
                }
                if (num4 < sharp.pointY)
                {
                    num4 = sharp.pointY + 5;
                }
                if (!sharp.isMouseOver)
                {
                    sharp.draw(g, this.ct, (Pen)Sharp.默认画笔.Clone());
                }
            }
            if (this.needNewItem)
            {
                foreach (SharpOffset offset in this.perItems)
                {
                    offset.sharp.draw(g, this.ct, Sharp.perLinePen);
                }
            }
        }
        private Dictionary<int, string> _strCGQAxleNum = new Dictionary<int, string>();
        
        public virtual void removeAllSelItem()
        {
            foreach (Sharp sharp in this.selItems)
            {
                this.ReDoStack.Push(OperType.List删除, this._allItems, this.allItems.IndexOf(sharp), sharp);
                sharp.isSel = false;
                ((List<Sharp>) this._allItems.value).Remove(sharp);
            }
            this.selItems.Clear();
        }

        public virtual void removeItem(Sharp s)
        {
            this.ReDoStack.Push(OperType.List删除, this._allItems, this.allItems.IndexOf(s), s);
            this.allItems.Remove(s);
            this.removeSel(s);
        }

        public virtual void removeSel(Sharp e)
        {
            this.selItems.Remove(e);
            e.isSel = false;
        }

        public int remove开关量(string 类型, string 子类型)
        {
            int num = 0;
            foreach (Sharp sharp in this.selItems)
            {
                if (类型.Equals(sharp.sharpType) && 子类型.Equals(sharp.subType))
                {
                    foreach (Digit digit in sharp.digitList)
                    {
                        digit.name = "";
                        digit.isLogic = false;
                        digit.ad = 0;
                        num++;
                    }
                }
            }
            return num;
        }

        public virtual void resetGrid()
        {
            if (this.scale > 128f)
            {
                this.scale = 128f;
            }
            this.ct.gridsize.Width = (int) (this.scale * this.ct.oraGridsize.Width);
            this.ct.gridsize.Height = (int) (this.scale * this.ct.oraGridsize.Height);
            this.ct.mapsize.Width = (int) (this.scale * this.ct.oraMapsize.Width);
            this.ct.mapsize.Height = (int) (this.scale * this.ct.oraMapsize.Height);
            if ((this.ct.gridsize.Width < 2) || (this.ct.gridsize.Height < 2))
            {
                this.ct.gridsize.Width = 2;
                this.ct.gridsize.Height = 2;
            }
        }

        public virtual void resetOffset(float rx, float ry, int sx, int sy)
        {
            float num = ((this.maxWorkArea.Height * this.scale) - (sy - toolbarHeight)) * ry;
            float num2 = ((this.maxWorkArea.Width * this.scale) - sx) * rx;
            float num3 = ((this.maxWorkArea.Height - this.ct.oraMapsize.Height) * this.scale) / 2f;
            float num4 = ((this.maxWorkArea.Width - this.ct.oraMapsize.Width) * this.scale) / 2f;
            this.ct.offsetX = -((int) (num2 - num4));
            this.ct.offsetY = -(((int) (num - num3)) - toolbarHeight);
            showArea.X = -((int) ((this.ct.offset.X * 1.0) / ((double) this.ct.gridsize.Width))) - 1;
            showArea.Y = -((int) ((this.ct.offset.Y * 1.0) / ((double) this.ct.gridsize.Height))) - 1;
            showArea.Width = (sx / this.ct.gridsize.Width) + 2;
            showArea.Height = (sx / this.ct.gridsize.Height) + 2;
        }
                
        public virtual void selAll()
        {
            this.selItems.Clear();
            foreach (Sharp sharp in this.allItems)
            {
                this.addSel(sharp);
            }
        }

        public virtual bool selOneType()
        {
            string sharpType = "";
            string subType = "";
            foreach (Sharp sharp in this.selItems)
            {
                sharpType = sharp.sharpType;
                subType = sharp.subType;
            }
            this.clearSel();
            foreach (Sharp sharp2 in this.allItems)
            {
                if (sharpType.Equals(sharp2.sharpType) && subType.Equals(sharp2.subType))
                {
                    this.addSel(sharp2);
                }
            }
            return true;
        }

        public virtual void selRect()
        {
            foreach (Sharp sharp in this.allItems)
            {
                if (!(((((((sharp.rectReal.Bottom * this.ct.gridsize.Height) / 2) + this.ct.offset.Y) > this.drawRect.Bottom) || ((((sharp.rectReal.Top * this.ct.gridsize.Height) / 2) + this.ct.offset.Y) < this.drawRect.Top)) || (((((sharp.rectReal.Left * this.ct.gridsize.Width) / 2) + this.ct.offset.X) < this.drawRect.Left) || ((((sharp.rectReal.Right * this.ct.gridsize.Width) / 2) + this.ct.offset.X) > this.drawRect.Right))) || sharp.isSel))
                {
                    this.addSel(sharp);
                }
            }
        }

        public virtual void showOrHideGrid()
        {
            this.ct.showGrid = !this.ct.showGrid;
        }

        public void SignModify()
        {
            this.ReDoStack.isModified = true;
        }

        public virtual void toBigger()
        {
            foreach (Sharp sharp in this.selItems)
            {
                sharp.addSize(1);
            }
        }

        public virtual void toSmaller()
        {
            foreach (Sharp sharp in this.selItems)
            {
                sharp.addSize(-1);
            }
        }

        public void Undo()
        {
            this.ReDoStack.UnDo();
        }

        public void WriteLinkNode()
        {
            try
            {
                CfgNode subNode = this.nodeRoot.GetSubNode("连接关系");
                if (subNode != null)
                {
                    CfgNode node2 = subNode.GetSubNode("区段连接关系", true);
                    CfgNode node3 = subNode.GetSubNode("信号机连接关系", true);
                    if ((node2.GetSubNodeList().Count > 0) || (node3.GetSubNodeList().Count > 0))
                    {
                        if (MessageBox.Show("是否覆盖连接关系？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            node2.ClearSubNodes();
                            node3.ClearSubNodes();
                        }
                        else
                        {
                            return;
                        }
                    }
                    CfgNode node4 = null;
                    int num = 0;
                    int num3 = 0;
                    int num4 = 0;
                    foreach (XHJ xhj in this.dictFx.Keys)
                    {
                        Quduan quduan;
                        num3++;
                        node4 = node3.GetSubNode("设备" + num3.ToString(), true);
                        node4["信号机名称"] = xhj.text;
                        if (this.dictFh.TryGetValue(xhj, out quduan))
                        {
                            node4["信号机内方第一区段"] = quduan.name;
                            node4["接近区段"] = "";
                        }
                        else
                        {
                            node4["信号机内方第一区段"] = "";
                            node4["接近区段"] = "";
                        }
                        if (this.dictJj.TryGetValue(xhj, out quduan))
                        {
                            node4["接近区段"] = quduan.name;
                        }
                        else
                        {
                            node4["接近区段"] = "";
                        }
                        node4["朝向"] = (this.dictFx[xhj] == 1) ? "左" : "右";
                        node4["ID"] = num3.ToString();
                    }
                    string[] strArray = null;
                    string[] strArray2 = new string[] { "区段名称", "左邻区段", "右邻区段", "道岔1", "道岔2", "道岔3", "道岔4", "道岔5" };
                    for (num = 0; num < this.allQuduan.Count; num++)
                    {
                        for (int i = 0; i < this.allQuduan[num].lst连接关系.Count; i++)
                        {
                            num4++;
                            node4 = node2.GetSubNode("设备" + ((num4 + 1)).ToString(), true);
                            strArray = this.allQuduan[num].lst连接关系[i].ToNodeString().Split(new char[] { ',' });
                            for (int j = 0; j < strArray2.Length; j++)
                            {
                                if (j < strArray.Length)
                                {
                                    node4[strArray2[j]] = strArray[j];
                                }
                                else
                                {
                                    node4[strArray2[j]] = "";
                                }
                            }
                            node4["ID"] = num4.ToString();
                        }
                    }
                    MessageBox.Show("写入成功！");
                }
            }
            catch (Exception exception)
            {
                Log.appendLog(exception.ToString());
            }
        }

        public int xhjKglCheck()
        {
            this.SignModify();
            WCQD wcqd = null;
            WCQD wcqd2 = null;
            StringBuilder builder = null;
            int num2 = 0;
            Digit digit = null;
            foreach (Sharp sharp in this.allItems)
            {
                builder = new StringBuilder();
                if ("sbXHJ".Equals(sharp.sharpType))
                {
                    int num4;
                    string name = "";
                    string str2 = "";
                    string str3 = "";
                    string str4 = "";
                    int ad = num4 = -1;
                    this.寻找信号机区段((XHJ) sharp, out wcqd, out wcqd2);
                    if (wcqd != null)
                    {
                        digit = wcqd.getDigit("WCQD_H");
                        if (digit != null)
                        {
                            name = digit.name;
                            str2 = digit.isLogic ? "1" : "0";
                            ad = digit.ad;
                        }
                    }
                    if (wcqd2 != null)
                    {
                        digit = wcqd2.getDigit("WCQD_H");
                        if (digit != null)
                        {
                            str3 = digit.name;
                            str4 = digit.isLogic ? "1" : "0";
                            num4 = digit.ad;
                        }
                    }
                    builder.Append(sharp.text).Append(',').Append(name).Append(',').Append(str2).Append(',').Append(ad).Append(',').Append(str3).Append(',').Append(str4).Append(',').Append(ad);
                    this.node信号机区段开关量.AddValue(num2.ToString(), builder.ToString());
                    num2++;
                }
            }
            foreach (Sharp sharp in this.allItems)
            {
                builder = new StringBuilder();
                if ("sbXHJ".Equals(sharp.sharpType))
                {
                    builder.Append(sharp.text).Append('=').Append(getXhjDigitId(this.nodeMap, sharp.text, 1)).Append(',').Append(getXhjDigitId(this.nodeMap, sharp.text, 0)).Append('\n');
                    File.AppendAllText("c:/aa.txt", builder.ToString());
                }
            }
            return 0;
        }

        public void 遍历区段(Sharp s, Point p)
        {
            List<Sharp> list = new List<Sharp>();
            DC item = null;
            WCQD wcqd = null;
            Point point = new Point(-1, -1);
            if ("sbDC".Equals(s.sharpType))
            {
                if ("26".Equals(s.text))
                {
                }
                this.临时道岔名.Add(s.text);
                item = (DC) s;
                item.岔后生成了么 = true;
            }
            else if ("sbWCQD".Equals(s.sharpType))
            {
                this.临时区段名.Add(s.text);
                wcqd = (WCQD) s;
                wcqd.岔后生成了么 = true;
            }
            this.临时区段控件.Add(s);
            foreach (Sharp sharp in this.allItems)
            {
                if ("C".Equals(s.text) && "26".Equals(sharp.text))
                {
                }
                if ("sbDC".Equals(sharp.sharpType))
                {
                    item = (DC) sharp;
                    if (!(item.岔后生成了么 || !sharp.hasPoint(p)))
                    {
                        list.Add(item);
                    }
                }
                if ("sbWCQD".Equals(sharp.sharpType))
                {
                    wcqd = (WCQD) sharp;
                    if (wcqd.text.Equals("33333"))
                    {
                    }
                    if (!(wcqd.岔后生成了么 || !sharp.hasPoint(p)))
                    {
                        list.Add(wcqd);
                    }
                }
                if ("sbJYJ".Equals(sharp.sharpType) && sharp.hasPoint(p))
                {
                    return;
                }
            }
            if (list.Count != 0)
            {
                foreach (Sharp sharp2 in list)
                {
                    if ("sbDC".Equals(sharp2.sharpType))
                    {
                        item = (DC) sharp2;
                        item.岔后生成了么 = true;
                        this.遍历区段(sharp2, item.getPoint('Q'));
                        this.遍历区段(sharp2, item.getPoint('D'));
                        this.遍历区段(sharp2, item.getPoint('F'));
                    }
                    else if ("sbWCQD".Equals(sharp2.sharpType))
                    {
                        wcqd = (WCQD) sharp2;
                        wcqd.岔后生成了么 = true;
                        this.遍历区段(sharp2, wcqd.getPoint('L'));
                        this.遍历区段(sharp2, wcqd.getPoint('R'));
                    }
                }
            }
        }

        public virtual void 岔前岔后自动生成()
        {
            this.SignModify();
            this.logicDigitsTemp.Clear();
            bool flag = false;
            if (!ChaHou.paramDict.TryGetValue(1, out flag))
            {
                flag = false;
            }
            if (flag)
            {
                this.clearAllLogic();
            }
            DC dc = null;
            for (int i = 0; i < this.array4checkLine.Length; i++)
            {
                for (int j = 0; j < this.array4checkLine[i].Length; j++)
                {
                    for (int k = 0; k < this.array4checkLine[i][j].Length; k++)
                    {
                        if (this.array4checkLine[i][j][k] > 1)
                        {
                            MessageBox.Show("检查到有压线,无法进行岔后生成!\r\n详情请看站场图上面的红色光带提示!");
                            return;
                        }
                    }
                }
            }
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbDC".Equals(sharp.sharpType))
                {
                    dc = (DC) sharp;
                    dc.岔后生成了么 = false;
                }
            }
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbDC".Equals(sharp.sharpType))
                {
                    dc = (DC) sharp;
                    if (!dc.岔后生成了么)
                    {
                        if ("56".Equals(dc.text) || "54".Equals(dc.text))
                        {
                        }
                        this.生成ing(dc);
                    }
                }
            }
            foreach (Logic logic in this.logicDigitsTemp.Values)
            {
                if (this.logicDigits.ContainsKey(logic.name))
                {
                    this.ReDoStack.Push(OperType.Dict删除, this._logicDigits, logic.name, this.logicDigits[logic.name]);
                    this.ReDoStack.Push(OperType.Dict添加, this._logicDigits, logic.name, logic);
                    this.logicDigits[logic.name] = logic;
                }
                else
                {
                    this.ReDoStack.Push(OperType.Dict添加, this._logicDigits, logic.name, logic);
                    this.logicDigits.Add(logic.name, logic);
                }
            }
            MessageBox.Show("岔后生成成功!");
        }

        private bool 岔前生成(DC 道岔GG, out string MMの前红, out bool MMの前红l, out string MMの前白, out bool MMの前白l)
        {
            bool flag = false;
            string str = 道岔GG.text + "DG";
            DC dc = 道岔GG;
            MMの前红 = dc.getDigit("DC_QH").name;
            MMの前白 = dc.getDigit("DC_QB").name;
            MMの前红l = dc.getDigit("DC_QH").isLogic;
            MMの前白l = dc.getDigit("DC_QB").isLogic;
            if ((!dc.getDigit("DC_QH").isLogic && (MMの前红 != null)) && (MMの前红.Trim().Length > 0))
            {
                str = MMの前红.Replace("-QH", "_QH").Replace("_QH", "DG");
            }
            if (MMの前红.Trim().Length == 0)
            {
                MMの前红 = dc.text + "-QH";
            }
            if (MMの前白.Trim().Length == 0)
            {
                MMの前白 = dc.text + "-QB";
            }
            Sharp sharp = dc;
            WCQD wcqd = null;
            Point point = dc.getPoint('Q');
            for (int i = 1; i <= 10; i++)
            {
                if ("sbDC".Equals(sharp.sharpType))
                {
                    dc = (DC) sharp;
                    point = dc.getPoint('Q');
                    sharp = this.向前探个路(dc, point);
                    if (sharp == dc)
                    {
                        return true;
                    }
                }
                else if ("sbWCQD".Equals(sharp.sharpType))
                {
                    wcqd = (WCQD) sharp;
                    flag = false;
                    if (!ChaHou.paramDict.TryGetValue(4, out flag))
                    {
                        flag = false;
                    }
                    if (flag)
                    {
                        wcqd.getDigit("WCQD_H").name = MMの前红;
                        wcqd.getDigit("WCQD_H").isLogic = MMの前红l;
                        wcqd.getDigit("WCQD_B").name = MMの前白;
                        wcqd.getDigit("WCQD_B").isLogic = MMの前白l;
                    }
                    flag = false;
                    if (!ChaHou.paramDict.TryGetValue(5, out flag))
                    {
                        flag = false;
                    }
                    if (flag)
                    {
                        wcqd.text = str;
                        wcqd.isHideTxt = true;
                    }
                    point = wcqd.getAnotherPoint(point);
                    sharp = this.向前探个路(wcqd, point);
                    if (sharp == wcqd)
                    {
                        return true;
                    }
                }
            }
            return true;
        }

        private void 递归(Sharp 最前面的MM, Sharp MM, string 前红, bool 逻辑前红, string 前白, bool 逻辑前白, Point 开始点, int 深度)
        {
            Sharp mM = null;
            DC dc = null;
            WCQD wcqd = null;
            Digit digit = null;
            Logic logic = null;
            string name = "";
            string str2 = "";
            string str3 = "";
            string str4 = "";
            bool flag = false;
            bool flag2 = false;
            bool flag3 = false;
            bool flag4 = false;
            if ("sbDC".Equals(MM.sharpType))
            {
                dc = (DC) MM;
                if (!开始点.Equals(dc.getPoint('Q')))
                {
                    return;
                }
                if (深度 >= 10)
                {
                    dc.岔后生成了么 = true;
                    return;
                }
                digit = dc.getDigit("DC_L");
                if (digit.name.Trim().Length == 0)
                {
                    digit.name = dc.text + "-L";
                    digit.isLogic = false;
                }
                digit = dc.getDigit("DC_U");
                if (digit.name.Trim().Length == 0)
                {
                    digit.name = dc.text + "-U";
                    digit.isLogic = false;
                }
                digit = dc.getDigit("DC_QH");
                if ((digit.name.Trim().Length == 0) && (最前面的MM != MM))
                {
                    digit.isLogic = true;
                }
                digit.name = 前红;
                digit = dc.getDigit("DC_QB");
                if ((digit.name.Trim().Length == 0) && (最前面的MM != MM))
                {
                    digit.isLogic = true;
                }
                digit.name = 前白;
                digit = dc.getDigit("DC_DH");
                if (digit.name.Trim().Length == 0)
                {
                    name = "L_" + dc.text + "DH";
                    flag = true;
                    logic = new Logic(name) {
                        code = "DC_DH",
                        d1 = 前红,
                        d1Logic = 逻辑前红,
                        d2 = dc.getDigit("DC_L").name,
                        d2Logic = dc.getDigit("DC_L").isLogic,
                        oper = 1
                    };
                    this.logicDigitsTemp.Remove(logic.name);
                    this.logicDigitsTemp.Add(logic.name, logic);
                }
                else
                {
                    name = digit.name;
                }
                digit = dc.getDigit("DC_DB");
                if (digit.name.Trim().Length == 0)
                {
                    str2 = "L_" + dc.text + "DB";
                    flag2 = true;
                    logic = new Logic(str2) {
                        code = "DC_DB",
                        d1 = 前白,
                        d1Logic = 逻辑前白,
                        d2 = dc.getDigit("DC_L").name,
                        d2Logic = dc.getDigit("DC_L").isLogic,
                        oper = 1
                    };
                    this.logicDigitsTemp.Remove(logic.name);
                    this.logicDigitsTemp.Add(logic.name, logic);
                }
                else
                {
                    str2 = digit.name;
                }
                digit = dc.getDigit("DC_FH");
                if (digit.name.Trim().Length == 0)
                {
                    str3 = "L_" + dc.text + "FH";
                    flag3 = true;
                    logic = new Logic(str3) {
                        code = "DC_FH",
                        d1 = 前红,
                        d1Logic = 逻辑前红,
                        d2 = dc.getDigit("DC_U").name,
                        d2Logic = dc.getDigit("DC_U").isLogic,
                        oper = 1
                    };
                    this.logicDigitsTemp.Remove(logic.name);
                    this.logicDigitsTemp.Add(logic.name, logic);
                }
                else
                {
                    str3 = digit.name;
                }
                digit = dc.getDigit("DC_FB");
                if (digit.name.Trim().Length == 0)
                {
                    str4 = "L_" + dc.text + "FB";
                    flag4 = true;
                    logic = new Logic(str4) {
                        code = "DC_FB",
                        d1 = 前白,
                        d1Logic = 逻辑前白,
                        d2 = dc.getDigit("DC_U").name,
                        d2Logic = dc.getDigit("DC_U").isLogic,
                        oper = 1
                    };
                    this.logicDigitsTemp.Remove(logic.name);
                    this.logicDigitsTemp.Add(logic.name, logic);
                }
                else
                {
                    str4 = digit.name;
                }
                dc.岔后生成了么 = true;
                mM = this.向前探个路(dc, dc.getPoint('D'));
                if (mM != dc)
                {
                    this.递归(最前面的MM, mM, name, flag, str2, flag2, dc.getPoint('D'), 深度++);
                }
                mM = this.向前探个路(dc, dc.getPoint('F'));
                if (mM != dc)
                {
                    this.递归(最前面的MM, mM, str3, flag3, str4, flag4, dc.getPoint('F'), 深度++);
                }
            }
            if ("sbWCQD".Equals(MM.sharpType))
            {
                wcqd = (WCQD) MM;
                if (深度 >= 10)
                {
                    wcqd.岔后生成了么 = true;
                }
                else
                {
                    digit = wcqd.getDigit("WCQD_H");
                    if (digit.name.Trim().Length == 0)
                    {
                        digit.name = 前红;
                        digit.isLogic = 逻辑前红;
                        if (逻辑前红)
                        {
                            logic = null;
                            if (this.logicDigitsTemp.TryGetValue(digit.name, out logic))
                            {
                                this.logicDigits.Remove(digit.name);
                                this.logicDigits.Add(digit.name, logic);
                            }
                        }
                    }
                    digit = wcqd.getDigit("WCQD_B");
                    if (digit.name.Trim().Length == 0)
                    {
                        digit.name = 前白;
                        digit.isLogic = 逻辑前白;
                        if (逻辑前白)
                        {
                            logic = null;
                            if (this.logicDigitsTemp.TryGetValue(digit.name, out logic))
                            {
                                this.logicDigits.Remove(digit.name);
                                this.logicDigits.Add(digit.name, logic);
                            }
                        }
                    }
                    wcqd.岔后生成了么 = true;
                    mM = this.向前探个路(wcqd, wcqd.getAnotherPoint(开始点));
                    if (mM != wcqd)
                    {
                        this.递归(最前面的MM, mM, 前红, 逻辑前红, 前白, 逻辑前白, wcqd.getAnotherPoint(开始点), 深度++);
                    }
                }
            }
        }

        public bool 区段变色笔(string 区段名称, Pen pen)
        {
            int num = 0;
            foreach (Sharp sharp in this.allItems)
            {
                if (("sbDC".Equals(sharp.sharpType) || "sbWCQD".Equals(sharp.sharpType)) && 区段名称.Equals(sharp.所属区段名称))
                {
                    dict区段颜色.Remove(sharp);
                    dict区段颜色.Add(sharp, pen);
                    num++;
                }
            }
            return (num > 0);
        }

        public int 区段扩展(Sharp s)
        {
            DC dc = null;
            WCQD wcqd = null;
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbDC".Equals(sharp.sharpType))
                {
                    dc = (DC) sharp;
                    dc.岔后生成了么 = false;
                }
                if ("sbWCQD".Equals(sharp.sharpType))
                {
                    wcqd = (WCQD) sharp;
                    wcqd.岔后生成了么 = false;
                }
            }
            this.clearSel();
            if ("sbDC".Equals(s.sharpType))
            {
                dc = (DC) s;
                this.遍历区段(s, dc.getPoint('Q'));
                this.遍历区段(s, dc.getPoint('D'));
                this.遍历区段(s, dc.getPoint('F'));
            }
            else if ("sbWCQD".Equals(s.sharpType))
            {
                wcqd = (WCQD) s;
                this.遍历区段(s, wcqd.getPoint('L'));
                this.遍历区段(s, wcqd.getPoint('R'));
            }
            return 1;
        }
           
        public int 全部联想(string 类型, string 子类型, Dictionary<string, string> lianxiangDict, Dictionary<string, bool> needLianxiangDict)
        {
            int num = 0;
            string str = "";
            bool flag = false;
            foreach (Sharp sharp in this.selItems)
            {
                if (类型.Equals(sharp.sharpType) && 子类型.Equals(sharp.subType))
                {
                    foreach (Digit digit in sharp.digitList)
                    {
                        if ((((lianxiangDict.TryGetValue(digit.code, out str) && needLianxiangDict.TryGetValue(digit.code, out flag)) && (((str != null) && (str.Length != 0)) && flag)) && ((sharp.text != null) && (sharp.text.Trim().Length != 0))) && ((digit.name == null) || (digit.name.Trim().Length == 0)))
                        {
                            digit.name = sharp.text.Trim() + str.Trim();
                        }
                    }
                    num++;
                }
            }
            return num;
        }

        private void 生成连接关系()
        {
            Quduan current;
            List<Quduan>.Enumerator enumerator2;
            this.dictJj.Clear();
            this.dictFh.Clear();
            this.dictFx.Clear();
            List<MapLinkRelation> list = new List<MapLinkRelation>();
            List<LinkRelationPoint> list2 = new List<LinkRelationPoint>();
            this.dictJyj.Clear();
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbJYJ".Equals(sharp.sharpType))
                {
                    Point point = sharp.getPoint('J');
                    if (this.dictJyj.ContainsKey((point.X * 0x2710) + point.Y))
                    {
                        this.dictJyj[(point.X * 0x2710) + point.Y] = (JYJ)sharp;
                    }
                    else
                    {
                        this.dictJyj.Add((point.X * 0x2710) + point.Y, (JYJ)sharp);
                    }
                }
            }
            using (enumerator2 = this.allQuduan.GetEnumerator())
            {
                while (enumerator2.MoveNext())
                {
                    current = enumerator2.Current;
                    current.get端点(this.dictJyj);
                }
            }
            int num = 0;
            int num2 = 0;
            int num3 = 0;
            int num4 = 0;
            int count = this.allQuduan.Count;
            list2.Clear();
            for (num = 0; num < (count - 1); num++)
            {
                Quduan quduan2 = this.allQuduan[num];
                int num5 = quduan2.lst区段端点.Count;
                for (num2 = num + 1; num2 < count; num2++)
                {
                    Quduan quduan3 = this.allQuduan[num2];
                    int num6 = quduan3.lst区段端点.Count;
                    for (num3 = 0; num3 < num5; num3++)
                    {
                        for (num4 = 0; num4 < num6; num4++)
                        {
                            if ((((num == 2) && (num2 == 3)) && (num3 == 0)) && (num4 == 1))
                            {
                            }
                            if (quduan2.lst区段端点[num3].point.Equals(quduan3.lst区段端点[num4].point))
                            {
                                int jiaodu = quduan2.lst区段端点[num3].jiaodu;
                                int num9 = quduan3.lst区段端点[num4].jiaodu;
                                if ((Math.Abs((int)(jiaodu - num9)) <= 0xff) && (Math.Abs((int)(jiaodu - num9)) >= 0x69))
                                {
                                    list2.Add(new LinkRelationPoint(num, num3, num2, num4));
                                }
                            }
                        }
                    }
                }
            }
            int num10 = list2.Count;
            XHJ key = null;
            Point point3 = new Point(-1, -1);
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbXHJ".Equals(sharp.sharpType))
                {
                    int num11;
                    key = (XHJ)sharp;
                    Point point2 = key.getPoint('P', out num11);
                    if ((num11 < 0x2d) || (num11 > 0xd7))
                    {
                        this.dictFx.Add(key, 1);
                    }
                    else
                    {
                        this.dictFx.Add(key, 0);
                    }
                    using (enumerator2 = this.allQuduan.GetEnumerator())
                    {
                        while (enumerator2.MoveNext())
                        {
                            current = enumerator2.Current;
                            if ("XKF".Equals(sharp.text) && "IIAG".Equals(current.name))
                            {
                            }
                            foreach (Duandian duandian in current.lst区段端点)
                            {
                                if (duandian.point.Equals(point2) && !point2.Equals(point3))
                                {
                                    int num12 = Math.Abs((int)(duandian.jiaodu - num11));
                                    if ((num12 < 70) || (num12 > 290))
                                    {
                                        if (!this.dictFh.ContainsKey(key))
                                        {
                                            this.dictFh.Add(key, current);
                                        }
                                    }
                                    else if (((num12 > 110) && (num12 < 250)) && !this.dictJj.ContainsKey(key))
                                    {
                                        this.dictJj.Add(key, current);
                                    }
                                }
                            }
                            if (!this.dictFh.ContainsKey(key) || !this.dictJj.ContainsKey(key))
                            {
                            }
                        }
                    }
                }
            }
            List<bool> list3 = null;
            int iQuduanNo = 0;
            int num15 = 0;
            int num16 = 0;
            int num19 = 0;
            int num20 = this.allQuduan.Count;
            num10 = list2.Count;
            list.Clear();
            List<int> list4 = new List<int>();
            List<int> list5 = new List<int>();
            for (iQuduanNo = 0; iQuduanNo < num20; iQuduanNo++)
            {
                current = this.allQuduan[iQuduanNo];
                num19 = current.lst可达组合.Count;
                for (num15 = 0; num15 < num19; num15++)
                {
                    int num18;
                    int iPointToIndex;
                    int iPointFromIndex;
                    int num27;
                    list3 = current.lst可达组合[num15].lst定反表;
                    int iDingfan = current.lst可达组合[num15].iDingfan;
                    Point point4 = current.lst区段端点[current.lst可达组合[num15].iPointFromIndex].point;
                    Point point5 = current.lst区段端点[current.lst可达组合[num15].iPointToIndex].point;
                    if ((point4.X > point5.X) || ((point4.X == point5.X) && (point4.Y > point5.Y)))
                    {
                        iPointToIndex = current.lst可达组合[num15].iPointToIndex;
                        iPointFromIndex = current.lst可达组合[num15].iPointFromIndex;
                    }
                    else
                    {
                        iPointToIndex = current.lst可达组合[num15].iPointFromIndex;
                        iPointFromIndex = current.lst可达组合[num15].iPointToIndex;
                    }
                    list4.Clear();
                    list5.Clear();
                    for (num16 = 0; num16 < num10; num16++)
                    {
                        int item = list2[num16].qd1;
                        int num25 = list2[num16].dd1;
                        int num24 = list2[num16].qd2;
                        int num26 = list2[num16].dd2;
                        if ((item == iQuduanNo) && (num25 == iPointToIndex))
                        {
                            list4.Add(num24);
                        }
                        else if ((num24 == iQuduanNo) && (num26 == iPointToIndex))
                        {
                            list4.Add(item);
                        }
                        if ((item == iQuduanNo) && (num25 == iPointFromIndex))
                        {
                            list5.Add(num24);
                        }
                        else if ((num24 == iQuduanNo) && (num26 == iPointFromIndex))
                        {
                            list5.Add(item);
                        }
                    }
                    if ((list4.Count == 0) && (list5.Count > 0))
                    {
                        num18 = 0;
                        while (num18 < list5.Count)
                        {
                            num27 = (this.allQuduan[iQuduanNo].lst区段端点[0].point.X * 0x3e8) + this.allQuduan[iQuduanNo].lst区段端点[0].point.Y;
                            list.Add(new MapLinkRelation(this.allQuduan, num27, iQuduanNo, -1, list5[num18], iDingfan, list3));
                            num18++;
                        }
                    }
                    else
                    {
                        int num17;
                        if ((list4.Count > 0) && (list5.Count == 0))
                        {
                            num17 = 0;
                            while (num17 < list4.Count)
                            {
                                num27 = (this.allQuduan[iQuduanNo].lst区段端点[0].point.X * 0x3e8) + this.allQuduan[iQuduanNo].lst区段端点[0].point.Y;
                                list.Add(new MapLinkRelation(this.allQuduan, num27, iQuduanNo, list4[num17], -1, iDingfan, list3));
                                num17++;
                            }
                        }
                        else
                        {
                            for (num17 = 0; num17 < list4.Count; num17++)
                            {
                                for (num18 = 0; num18 < list5.Count; num18++)
                                {
                                    num27 = (this.allQuduan[iQuduanNo].lst区段端点[0].point.X * 0x3e8) + this.allQuduan[iQuduanNo].lst区段端点[0].point.Y;
                                    list.Add(new MapLinkRelation(this.allQuduan, num27, iQuduanNo, list4[num17], list5[num18], iDingfan, list3));
                                }
                            }
                        }
                    }
                }
            }
            foreach (Sharp sharp2 in this.allItems)
            {
                if ("sbDC".Equals(sharp2.sharpType))
                {
                    ((DC)sharp2).定反位 = '无';
                }
            }
            Dictionary<ulong, MapLinkRelation> dictionary = new Dictionary<ulong, MapLinkRelation>();
            foreach (MapLinkRelation relation in list)
            {
                if (!dictionary.ContainsKey((ulong)((relation.iQuduanNo * 0x100000000L) + relation.iDingfanSort)))
                {
                    dictionary.Add((ulong)((relation.iQuduanNo * 0x100000000L) + relation.iDingfanSort), relation);
                }
            }
        }

        private void 生成ing(DC 道岔GG)
        {
            string str;
            string str2;
            bool flag;
            bool flag2;
            DC dc = this.寻找MM(道岔GG);
            this.搜索光带(dc, out str, out flag, out str2, out flag2);
            this.岔前生成(dc, out str, out flag, out str2, out flag2);
            Point point = dc.getPoint('Q');
            this.递归(dc, dc, str, flag, str2, flag2, point, 1);
        }
        
        private bool 搜索光带(DC 道岔GG, out string MMの前红, out bool MMの前红l, out string MMの前白, out bool MMの前白l)
        {
            DC dc = 道岔GG;
            MMの前红 = dc.getDigit("DC_QH").name;
            MMの前白 = dc.getDigit("DC_QB").name;
            MMの前红l = dc.getDigit("DC_QH").isLogic;
            MMの前白l = dc.getDigit("DC_QB").isLogic;
            if ((MMの前红.Trim().Length > 0) && (MMの前白.Trim().Length > 0))
            {
                return true;
            }
            Sharp sharp = dc;
            WCQD wcqd = null;
            Point point = dc.getPoint('Q');
            for (int i = 1; i <= 10; i++)
            {
                if ("sbDC".Equals(sharp.sharpType))
                {
                    dc = (DC) sharp;
                    point = dc.getPoint('Q');
                    sharp = this.向前探个路(dc, point);
                    if (sharp == wcqd)
                    {
                        return false;
                    }
                }
                else if ("sbWCQD".Equals(sharp.sharpType))
                {
                    wcqd = (WCQD) sharp;
                    MMの前红 = wcqd.getDigit("WCQD_H").name;
                    MMの前红l = wcqd.getDigit("WCQD_H").isLogic;
                    MMの前白 = wcqd.getDigit("WCQD_B").name;
                    MMの前白l = wcqd.getDigit("WCQD_B").isLogic;
                    if ((MMの前红.Trim().Length > 0) && (MMの前白.Trim().Length > 0))
                    {
                        return true;
                    }
                    point = wcqd.getAnotherPoint(point);
                    sharp = this.向前探个路(wcqd, point);
                    if (sharp == wcqd)
                    {
                        return false;
                    }
                }
            }
            return false;
        }

        private Sharp 向前探个路(Sharp 乖乖, Point 出发点)
        {
            Sharp sharp = 乖乖;
            foreach (Sharp sharp2 in this.allItems)
            {
                if (sharp2.hasPoint(出发点) && (sharp2 != 乖乖))
                {
                    if ("sbJYJ".Equals(sharp2.sharpType) || "sbXHJ".Equals(sharp2.sharpType))
                    {
                        return 乖乖;
                    }
                    if ("sbWCQD".Equals(sharp2.sharpType) || "sbDC".Equals(sharp2.sharpType))
                    {
                        sharp = sharp2;
                    }
                }
            }
            return sharp;
        }

        public bool 信号机变色笔(string 信号机名称, Pen pen)
        {
            int num = 0;
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbXHJ".Equals(sharp.sharpType) && 信号机名称.Equals(sharp.text))
                {
                    dict区段颜色.Remove(sharp);
                    dict区段颜色.Add(sharp, pen);
                    num++;
                }
            }
            return (num > 0);
        }

        public virtual void 旋转180度()
        {
            if (this.selItems.Count > 0)
            {
                this.ReDoStack.Push(new OperElement("旋转180度"));
                int x = 0x2710;
                int y = 0x2710;
                int num3 = -10000;
                int num4 = -10000;
                foreach (Sharp sharp in this.selItems)
                {
                    if (sharp.pointX < x)
                    {
                        x = sharp.point.X;
                    }
                    if (sharp.pointY < y)
                    {
                        y = sharp.point.Y;
                    }
                    if ((sharp.pointX + sharp.sizeDefineWidth) > num3)
                    {
                        num3 = sharp.pointX + sharp.sizeDefineWidth;
                    }
                    if ((sharp.pointY + sharp.sizeDefineHeight) > num4)
                    {
                        num4 = sharp.pointY + sharp.sizeDefineHeight;
                    }
                }
                Point point = new Point(x + num3, y + num4);
                foreach (Sharp sharp in this.selItems)
                {
                    sharp.xuanzhuan180(point);
                }
            }
        }

        private DC 寻找MM(DC 道岔GG)
        {
            DC dc = 道岔GG;
            Sharp sharp = dc;
            Sharp sharp2 = sharp;
            WCQD wcqd = null;
            Point point = dc.getPoint('Q');
            bool flag = true;
            for (int i = 1; i <= 10; i++)
            {
                if ("sbDC".Equals(sharp.sharpType))
                {
                    if (flag)
                    {
                        flag = false;
                    }
                    else if (point.Equals(((DC) sharp).getPoint('Q')))
                    {
                        return dc;
                    }
                    dc = (DC) sharp;
                    sharp2 = dc;
                    point = dc.getPoint('Q');
                    sharp = this.向前探个路(dc, point);
                    if (sharp == dc)
                    {
                        return dc;
                    }
                }
                else if ("sbWCQD".Equals(sharp.sharpType))
                {
                    wcqd = (WCQD) sharp;
                    sharp2 = wcqd;
                    point = wcqd.getAnotherPoint(point);
                    sharp = this.向前探个路(wcqd, point);
                    if (sharp == wcqd)
                    {
                        return dc;
                    }
                }
            }
            return dc;
        }

        private bool 寻找信号机区段(XHJ GG, out WCQD 接近MM, out WCQD 防护MM)
        {
            接近MM = null;
            防护MM = null;
            WCQD wcqd = null;
            Point pt = GG.getPoint('P');
            Point point2 = new Point();
            foreach (Sharp sharp in this.allItems)
            {
                if ("sbWCQD".Equals(sharp.sharpType) && sharp.hasPoint(pt))
                {
                    wcqd = (WCQD) sharp;
                    point2 = wcqd.getAnotherPoint(pt);
                    if (GG.direction == 2)
                    {
                        if (point2.X > pt.X)
                        {
                            接近MM = wcqd;
                        }
                        else
                        {
                            防护MM = wcqd;
                        }
                    }
                    else if (GG.direction == 3)
                    {
                        if (point2.Y > pt.Y)
                        {
                            接近MM = wcqd;
                        }
                        else
                        {
                            防护MM = wcqd;
                        }
                    }
                    else if (GG.direction == 1)
                    {
                        if (point2.Y < pt.Y)
                        {
                            接近MM = wcqd;
                        }
                        else
                        {
                            防护MM = wcqd;
                        }
                    }
                    else if (point2.X < pt.X)
                    {
                        接近MM = wcqd;
                    }
                    else
                    {
                        防护MM = wcqd;
                    }
                }
            }
            return ((接近MM != null) && (防护MM != null));
        }

        public virtual void 有效信号逻辑生成()
        {
            this.SignModify();
            this.logicDigitsTemp.Clear();
            string name = "";
            string dtype = "";
            Logic logic = null;
            Digit item = null;
            List<Digit> list = new List<Digit>();
            StringBuilder builder = new StringBuilder("以下信号机没有找到任何绿、黄开关量：");
            int num = 0;
            foreach (Sharp sharp in this.allItems)
            {
                list.Clear();
                if ("sbXHJ".Equals(sharp.sharpType) && ((("2".Equals(sharp.subType) || "3".Equals(sharp.subType)) || ("4".Equals(sharp.subType) || "5".Equals(sharp.subType))) || "6".Equals(sharp.subType)))
                {
                    if ("STF".Equals(sharp.text))
                    {
                    }
                    dtype = "XHJ_L";
                    item = sharp.getDigit(dtype);
                    if ((null != item) && (item.name.Trim().Length > 0))
                    {
                        list.Add(item);
                    }
                    dtype = "XHJ_U";
                    item = sharp.getDigit(dtype);
                    if ((null != item) && (item.name.Trim().Length > 0))
                    {
                        list.Add(item);
                    }
                    dtype = "XHJ_2L";
                    item = sharp.getDigit(dtype);
                    if ((null != item) && (item.name.Trim().Length > 0))
                    {
                        list.Add(item);
                    }
                    dtype = "XHJ_2U";
                    item = sharp.getDigit(dtype);
                    if ((null != item) && (item.name.Trim().Length > 0))
                    {
                        list.Add(item);
                    }
                    dtype = "XHJ_USU";
                    item = sharp.getDigit(dtype);
                    if ((null != item) && (item.name.Trim().Length > 0))
                    {
                        list.Add(item);
                    }
                    dtype = "XHJ_LU";
                    item = sharp.getDigit(dtype);
                    if ((null != item) && (item.name.Trim().Length > 0))
                    {
                        list.Add(item);
                    }
                    if (list.Count == 0)
                    {
                        num++;
                        builder.Append(sharp.text).Append(0xff0c);
                    }
                    else
                    {
                        Digit[] digitArray = list.ToArray();
                        name = "XHJ_" + sharp.text + "_1";
                        logic = new Logic(name) {
                            d1 = digitArray[0].name,
                            d2 = digitArray[0].name,
                            d3 = digitArray[0].name,
                            d1Logic = digitArray[0].isLogic,
                            d2Logic = digitArray[0].isLogic,
                            d3Logic = digitArray[0].isLogic,
                            d1Not = false,
                            d2Not = false,
                            d3Not = false,
                            logic = null,
                            oper = 2
                        };
                        if (digitArray.Length >= 1)
                        {
                            logic.d1 = digitArray[0].name;
                            logic.d1Logic = digitArray[0].isLogic;
                        }
                        if (digitArray.Length >= 2)
                        {
                            logic.d2 = digitArray[1].name;
                            logic.d2Logic = digitArray[1].isLogic;
                        }
                        if (digitArray.Length >= 3)
                        {
                            logic.d3 = digitArray[2].name;
                            logic.d3Logic = digitArray[2].isLogic;
                        }
                        this.logicDigitsTemp.Add(name, logic);
                        name = "XHJ_" + sharp.text + "_2";
                        logic = new Logic(name) {
                            d1 = digitArray[0].name,
                            d2 = digitArray[0].name,
                            d3 = digitArray[0].name,
                            d1Logic = digitArray[0].isLogic,
                            d2Logic = digitArray[0].isLogic,
                            d3Logic = digitArray[0].isLogic,
                            d1Not = false,
                            d2Not = false,
                            d3Not = false,
                            logic = null,
                            oper = 2
                        };
                        if (digitArray.Length >= 4)
                        {
                            logic.d1 = digitArray[3].name;
                            logic.d1Logic = digitArray[3].isLogic;
                        }
                        if (digitArray.Length >= 5)
                        {
                            logic.d2 = digitArray[4].name;
                            logic.d2Logic = digitArray[4].isLogic;
                        }
                        if (digitArray.Length >= 6)
                        {
                            logic.d3 = digitArray[5].name;
                            logic.d3Logic = digitArray[5].isLogic;
                        }
                        this.logicDigitsTemp.Add(name, logic);
                        name = "XHJ_" + sharp.text + "_OPEN";
                        logic = new Logic(name) {
                            d1 = "XHJ_" + sharp.text + "_1",
                            d2 = "XHJ_" + sharp.text + "_2",
                            d3 = "",
                            d1Logic = true,
                            d2Logic = true,
                            d3Logic = false,
                            d1Not = false,
                            d2Not = false,
                            d3Not = false,
                            logic = null,
                            oper = 2
                        };
                        this.logicDigitsTemp.Add(name, logic);
                    }
                }
            }
            foreach (Logic logic2 in this.logicDigitsTemp.Values)
            {
                if (this.logicDigits.ContainsKey(logic2.name))
                {
                    this.ReDoStack.Push(OperType.Dict删除, this._logicDigits, logic2.name, this.logicDigits[logic2.name]);
                    this.ReDoStack.Push(OperType.Dict添加, this._logicDigits, logic2.name, logic2);
                    this.logicDigits[logic2.name] = logic2;
                }
                else
                {
                    this.ReDoStack.Push(OperType.Dict添加, this._logicDigits, logic2.name, logic2);
                    this.logicDigits.Add(logic2.name, logic2);
                }
            }
            if (num > 0)
            {
                builder.Append("请补充完善之后重新生成！");
                MessageBox.Show(builder.ToString());
            }
            else
            {
                MessageBox.Show("生成成功!");
            }
        }

        public List<Sharp> allItems
        {
            get
            {
                return (List<Sharp>) this._allItems.value;
            }
            set
            {
                this._allItems.value = value;
            }
        }

        public Dictionary<string, Logic> logicDigits
        {
            get
            {
                return (Dictionary<string, Logic>) this._logicDigits.value;
            }
            set
            {
                this._logicDigits.value = value;
            }
        }
    }
}

